layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin;

    /**
     * 防重复设置
     * @type {boolean}
     */
     var  add_user = false;

     var  add_quota = false;


    /**
     * 初始化 等级下拉选项
     */
    initAgencyLevelSelect();
    table.render({
        elem: '#agency_agencyUser_table'
        ,url: '/agency_user/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
             {field:'id', title: 'id',width:100, "align":"center"}
            ,{field:'userName',width:200, title: '用户名',"align":"center"}
            ,{field:'mobile',width:200, title: '手机号码',"align":"center"}
            ,{field:'level',width:200, title: '等级',"align":"center",templet:'<span>{{d.agencyLevel.level}}</span>'}
            ,{field:'name',width:200, title: '等级名称',"align":"center",templet:'<span>{{d.agencyLevel.name}}</span>'}
            ,{field:'createdTime',width:200, title: '注册时间',"align":"center"}
            ,{field:'OPT', width:300, title: '操作',"align":"center",toolbar:"#OptData"}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{
            userName:$(".queryTable input[name='userName']").val(),
            mobile:$(".queryTable input[name='mobile']").val()
        }
        ,id: 'agency_agencyUser_table'
        ,page: true
    });

    table.on('tool(agency_agencyUser_table)', function(obj){

        var table_event = obj.event;

        var data = obj.data;

        $("#add_agencyUser_form select[name='levelId']").attr("disabled","true");
        $("#add_agencyUser_form input[name='userName']").attr("disabled","true");
        $("#add_agencyUser_form input[name='mobile']").attr("disabled","true");


        if(table_event=="modify_agencyUser"){
            add_user = false;
            admin.popup({
                title: '编辑'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#add_agencyUser_win")
                ,end:function () {
                    table.reload('agency_agencyUser_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                        }
                    });
                }
            });
            form.val('add_agencyUser_form', {
                "id":data.id,
                "userName":data.userName,
                "mobile": data.mobile,
                "levelId":data.levelId
            });

        }else if(table_event=="add_quota"){
            $("#add_quota_form")[0].reset();
            add_quota = false;
            admin.popup({
                title: '添加名额'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#add_quota_win")
                ,end:function () {

                }
            });

            form.val('add_quota_form', {
                "applyUserId":data.id,
                "userName":data.userName,
                "levelId":data.levelId
            });
        }

    });

    //添加代理商
    $(".agencyUserAdd_btn").click(function(){

        add_user = false;
        /**
             * 初始化 等级下拉
             */


        $("#add_agencyUser_form")[0].reset();
        $("#add_agencyUser_form select[name='levelId']").removeAttr("disabled");
        $("#add_agencyUser_form input[name='userName']").removeAttr("disabled");
        $("#add_agencyUser_form input[name='mobile']").removeAttr("disabled");


        admin.popup({
                title: '编辑'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#add_agencyUser_win")
                ,end:function () {
                    table.reload('agency_agencyUser_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                            ,where: {
                                userName:$(".queryTable input[name='userName']").val(),
                                mobile:$(".queryTable input[name='mobile']").val()
                            }
                        }
                    });
                }
            });
        form.val('add_agencyUser_form', {
            "id":""
        })

    });

    /**
     * oem 新增提交
     */
    form.on('submit(addagencyUserOk)',function (data) {
        if(!add_user){
            add_user= true;
            $.ajax({
                url:"/agency_user/edit",
                data:data.field,
                type:"post",
                success:function (result) {
                    if (result.code == 1){
                        layer.msg("编辑成功",{
                            offset: '32%'
                            ,icon: 1
                            ,time: 0
                            ,btn: '知道了'
                            ,btnAlign:"c"
                            ,shade:0.4
                            ,yes:function () {
                                table.reload('agency_agencyUser_table', {
                                    where: {
                                        userName:$(".queryTable input[name='userName']").val(),
                                        mobile:$(".queryTable input[name='mobile']").val()

                                    }
                                });
                                layer.closeAll();
                            }
                        })
                    }else{
                        add_user=false;
                        layer.msg(result.message,{
                            offset: '32%'
                            ,icon: 2
                            ,time: 3000
                        },function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        }else {
            layer.msg("不能重复提交表单",{
                offset: '32%'
                ,icon: 2
                ,time: 3000
            },function () {

            })
        }

        return false;
    });

    /**
     * 直接添加名额
     */
    form.on('submit(addQuotaOk)',function (data) {
        if(!add_quota){
            add_quota = true;
            $.ajax({
                url:"/quota_apply_oem/add_quota",
                data:data.field,
                type:"post",
                success:function (result) {
                    if (result.code == 1){
                        layer.msg("添加名额成功",{
                            offset: '32%'
                            ,icon: 1
                            ,time: 0
                            ,btn: '知道了'
                            ,btnAlign:"c"
                            ,shade:0.4
                            ,yes:function () {

                                table.reload('agency_agencyUser_table', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    ,where: {
                                        userName:$(".queryTable input[name='userName']").val(),
                                        mobile:$(".queryTable input[name='mobile']").val()

                                    }
                                });
                                layer.closeAll();
                            }
                        })
                    }else{
                        add_quota = false;
                        layer.msg(result.message,{
                            offset: '32%'
                            ,icon: 2
                            ,time: 3000
                        },function () {
                            layer.closeAll();
                        })
                    }

                }
            });
        }else {
            layer.msg("不能重复提交表单",{
                offset: '32%'
                ,icon: 2
                ,time: 3000
            },function () {

            });

        }

        return false;
    });
    
    $(".search_btn").click(function () {

        table.reload('agency_agencyUser_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                userName:$(".queryTable input[name='userName']").val(),
                mobile:$(".queryTable input[name='mobile']").val()

            }
        });

    });
    
});

/**
 * 动态获取等级
 */
function initAgencyLevelSelect() {
    $.post(
        "/agency_level/queryData",
        {},
        function (result) {

            if(result.code==1){

                $('.level_form select[name="levelId"] option').remove();
                $.each(result.data, function (index, item) {
                        $('.level_form select[name="levelId"]').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                });

            }else {
                layer.msg(result.message,{
                    offset: '32%'
                    ,icon: 2
                    ,time: 3000
                },function () {
                    layer.closeAll();
                })
            }
        }
    );
};

